package com.qdu.controller;

import com.qdu.entity.Houseinfo;
import com.qdu.entity.Reportinfo;
import com.qdu.service.HouseService;
import com.qdu.service.ReportService;
import com.qdu.util.GenerateUUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/reports")
public class ReportController {

    @Autowired
    private ReportService reportService;

    @Autowired
    HouseService houseService;

    @PostMapping("/delete")
    public void deleteByPrimaryKey(String rid){
        reportService.deleteByPrimaryKey(rid);
    }

    @PostMapping("/add")
    public Result insert(@RequestBody Reportinfo record){
        System.out.println(record);
        Result result = new Result();
        record.setAid("-1");
        record.setRid("R"+ GenerateUUID.generateUUID());
        record.setState(2);
        reportService.insert(record);
        result.setMsg("举报成功");
        return result;
    }

    @PostMapping("/addselective")
    public void insertSelective(Reportinfo record){
        reportService.insertSelective(record);
    }

    @GetMapping("/getbyrid")
    public Reportinfo selectByPrimaryKey(String rid){
        return reportService.selectByPrimaryKey(rid);
    }

    @GetMapping("/getbyuid")
    public List selectByUid(String uid){
        System.out.println(uid);
        List<Reportinfo> reports = reportService.selectByUid(uid);
        List<Map> infos = new ArrayList();

        for(Reportinfo r : reports){
            List<String> imgs=new ArrayList<>();
            Map<String,Object> info = new HashMap<>();
            Houseinfo houseinfo = houseService.selectByHid(r.getHid());
            String img=houseinfo.getHimg();
            String[] is=img.split("!");
            for (String i:is){
                imgs.add(i);
            }
            info.put("house", houseinfo);
            info.put("report", r);
            info.put("img", imgs.get(0));
            infos.add(info);
        }
        return infos;
    }

    @GetMapping("/getbyhid")
    public Reportinfo selectByHid(String hid){
        return reportService.selectByHid(hid);
    }

    @PostMapping("/updateselective")
    public void updateByPrimaryKeySelective(Reportinfo record){
        reportService.updateByPrimaryKeySelective(record);
    }

    @PostMapping("/update")
    public void updateByPrimaryKey(Reportinfo record){
        reportService.updateByPrimaryKey(record);
    }
}
